package com.efounder.form.comp;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.DataSetListener;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.form.DMColComponent;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.util.AppContext;
import org.angmarch.views.NiceSpinner;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * by lch
 * note:popuwindow有个位置属性，在父类中----location
 * 购物车没有调用setdataset方法。
 */
public class FormSpinner extends NiceSpinner implements IComponent,IUIComponent,DMComponent,DMColComponent,DataSetListener {


	private String[] keyArray;
	private String[] valueArray;

	//避免datachanged重复循环
	private String currentSelectedItem;

	private IUIComponent parentComp;
	//自己更改mainrowset时，无需触发datasetchange事件.
	private boolean isSelfDataSetChange = false;

	public FormSpinner() {
		super(AppContext.getCurrentActivity());
	}

	public FormSpinner(Context context) {
		super(context);
	}

	public FormSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * NiceSpinner组件比较简单，dataChange方法并没有在onitemclick时处理。此方法只有在外部datasetchange的时候才会触发。
	 * @param dataSet
     */
	public void dataChanged(EFDataSet dataSet) {
		if(!isSelfDataSetChange) {
			mainRowSet = dataSet.getCurrentRowSet();
			String key = "";
			if (mainRowSet != null) {
				key = mainRowSet.getString(dataSetColID, "");
			}
			for (int i = 0; i < keyArray.length; i++) {
				String valueOfArray = keyArray[i];
				if (valueOfArray.equals(key))
					this.setSelectedIndex(i);
			}
		}
	}
	public void setInitValue(String initKey){
		if(keyArray==null)
			return;
		for (int i = 0; i < keyArray.length; i++) {
			String valueOfArray = keyArray[i];
			if (valueOfArray.equals(initKey))
				this.setSelectedIndex(i);
		}
		if (mainRowSet != null) {
			mainRowSet.put(dataSetColID, initKey);
		}
	}

	@Override
	public DataSetComponent getDataSetComponent() {
		return dataSetComponent;
	}

	@Override
	public void setDataSetComponent(DataSetComponent dsc) {
		if (dataSetComponent != null)
			dataSetComponent.removeDMComponent(this);
		dataSetComponent = dsc;
		if (dataSetComponent != null)
			dataSetComponent.insertDMComponent(this);
	}

	@Override
	public void setLayout() {
		FormCompUtil.setLayoutSize(this);
//		this.setGravity(FormCompUtil.getLayoutGravity(horizontalAlign, verticalAlign));
		this.setGravity(Gravity.LEFT| Gravity.CENTER);
	}

	@Override
	public void creationComplete() {

		this.setTextColor(getResources().getColor(android.R.color.black));
		if (valueArray == null) return;
		List<String> dataset = new LinkedList<String>(Arrays.asList(valueArray));
		this.attachDataSource(dataset);

		//默认透明背景色
		setBackgroundColor(0x00FFFFFF);
		this.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if(mainRowSet!=null)
				mainRowSet.putString(dataSetColID,keyArray[position]);
				currentSelectedItem = keyArray[position];
				isSelfDataSetChange = true;
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
	}


	@Override
	public void setDataSetID(String dataSetID) {
		this.dataSetID = dataSetID;
	}

	@Override
	public String getDataSetColID() {
		return dataSetColID;
	}

	@Override
	public void setDataSetColID(String dataSetColID) {
		this.dataSetColID = dataSetColID;
	}

	@Override
	public String getDataSetNameColID() {
		return null;
	}

	@Override
	public void setDataSetNameColID(String value) {

	}

	@Override
	public String getInternalDataSetID() {
		return null;
	}

	@Override
	public void setInternalDataSetID(String dataSetID) {

	}

	@Override
	public void setViewDataSetID(String viewDataSetID) {

	}

	@Override
	public String getViewDataSetID() {
		return null;
	}

	@Override
	public void setViewDataSetColID(String viewDataSetColID) {

	}

	@Override
	public String getViewDataSetColID() {
		return null;
	}

	@Override
	public void setValueDataSetColID(String valueDataSetColID) {

	}

	@Override
	public String getValueDataSetColID() {
		return null;
	}

	@Override
	public Boolean getIsUserInternalDataSetID() {
		return null;
	}

	@Override
	public void setIsUserInternalDataSetID(Boolean v) {

	}

	@Override
	public String getIdentifier() {
		return null;
	}

	@Override
	public Object getPropertyMap() {
		return null;
	}

	@Override
	public void setPropertyMap(Object value) {

	}

	@Override
	public Object getProperty(String key, Object defVal) {
		return null;
	}

	@Override
	public void setProperty(String key, Object value) {

	}

	@Override
	public int getNumberPrecision() {
		return 0;
	}

	@Override
	public void seNumberPrecision(int value) {

	}

	@Override
	public String getNumberFormat() {
		return null;
	}

	@Override
	public void setNumberFormat(String numberFormat) {

	}

	@Override
	public String getColumnType() {
		return null;
	}

	@Override
	public void setColumnType(String value) {

	}

	@Override
	public String getEditorType() {
		return null;
	}

	@Override
	public void setEditorType(String value) {

	}
	@Override
	public String getDateFormat() {
		return null;
	}

	@Override
	public void setDateFormat(String dateFormat) {

	}

	@Override
	public String getFormulaOne() {
		return null;
	}

	@Override
	public void setFormulaOne(String formulaOne) {

	}



	@Override
	public String getFkeyID() {
		return null;
	}

	@Override
	public void setFkeyID(String fkey) {

	}

	@Override
	public String getRlglID() {
		return null;
	}

	@Override
	public void setRlglID(String rlglID) {

	}

	@Override
	public String getDataSetID() {
		return dataSetID;
	}

	@Override
	public void setDataSet(EFDataSet dataSet) {
		if ( this.dataSet != dataSet ) {
			if ( this.dataSet != null ) this.dataSet.removeDataSetListener(this);
			this.dataSet = dataSet;
			if ( this.dataSet != null ) this.dataSet.addDataSetListener(this);
			this.dataChanged(dataSet);
		}
	}

	@Override
	public EFDataSet getDataSet() {
		return dataSet;
	}

	@Override
	public EFRowSet getMainRowSet() {
		return mainRowSet;
	}

	@Override
	public void dataSetChanged(DataSetEvent event) {
		dataChanged(event.getDataSet());
	}

	@Override
	public String getID() {
		return id;
	}

	@Override
	public void setID(String id) {
		this.id = id;
	}

	@Override
	public int getGap() {
		return gap;
	}

	@Override
	public void setGap(int gap) {
		this.gap = gap;
	}


	@Override
	public void setPercentWidth(int percentW) {
		percentWidth = percentW;
	}

	@Override
	public int getPercentHeight() {
		return percentHeight;
	}

	@Override
	public void setPercentHeight(int percentH) {
		percentHeight = percentH;
	}

	@Override
	public ViewSize getViewSize() {
		return size;
	}

	@Override
	public void setViewSize(ViewSize size) {
		this.size = size;
	}

	@Override
	public String getEditMask() {
		return null;
	}

	@Override
	public void setEditMask(String editMask) {
		this.editMask = editMask;
		String[] datamodelArray = editMask.split(";");
		if(datamodelArray.length>0){
			keyArray = new String[datamodelArray.length];
			valueArray = new String[datamodelArray.length];
			for(int i = 0;i<datamodelArray.length;i++){


				String key =  datamodelArray[i].substring(0,datamodelArray[i].indexOf(":"));
				String value = datamodelArray[i].substring(datamodelArray[i].indexOf(":")+1,datamodelArray[i].length());
				//String value = datamodelArray[i];
				keyArray[i] = key;
				valueArray[i] = value;

			}
		}

	}
	@Override
	public void setCompWidth(int width) {
		this.width = width;
	}

	@Override
	public int getCompWidth(){
		return this.width;
	}

	@Override
	public void setCompHeight(int height) {
		this.height = height;
	}

	@Override
	public int getCompHeight() {
		return this.height;
	}

	private String id;
	private float x = 0;
	private float y = 0;
	private int width;
	private int height;
	private Boolean visible;
	private int tabIndex = 0;
	private int fontColor = 0;
	private int fontSize = 12;


	private String editMask;
	/**
	 * 布局类型：垂直vertical、水平horizontal
	 */
	private String layoutType = "horizontal";
	private int requestColCount;
	private double horizontalGap;
	private double verticalGap;

	private int percentWidth;

	private int percentHeight;

	private int gap = 0;

	private ViewSize size = new ViewSize(0,0);

	private String dataSetID;
	private String dataSetColID;

	private String horizontalAlign;
	private String verticalAlign;

	private DataSetComponent dataSetComponent;
	private EFDataSet dataSet;
	private EFRowSet mainRowSet;


	public int getPercentWidth() {
		return percentWidth;
	}

	public void setId(String id) {
		this.id = id;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setY(float y){

	}




	public Boolean getVisible() {
		return visible;
	}

	public void setVisible(Boolean visible) {
		this.visible = visible;

	}

	public int getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(int tabIndex) {
		if(tabIndex != -1) {
			this.tabIndex = tabIndex;
		}

	}




	public String getLayoutType() {
		return layoutType;
	}

	public void setLayoutType(String layoutType) {
		this.layoutType = layoutType;
	}

	public int getRequestColCount() {
		return requestColCount;
	}

	public void setRequestColCount(int requestColCount) {
		this.requestColCount = requestColCount;
	}

	public double getHorizontalGap() {
		return horizontalGap;
	}

	public void setHorizontalGap(double horizontalGap) {
		this.horizontalGap = horizontalGap;
	}

	public double getVerticalGap() {
		return verticalGap;
	}

	public void setVerticalGap(double verticalGap) {
		this.verticalGap = verticalGap;
	}

	public int getFontColor() {
		return fontColor;
	}
	public void setFontColor(int fontColor) {
		this.fontColor = fontColor;
	}
	public int getFontSize() {
		return fontSize;
	}
	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	public String getCurrentSelectedItem() {
		return currentSelectedItem;
	}

	public void setCurrentSelectedItem(String currentSelectedItem) {
		this.currentSelectedItem = currentSelectedItem;
	}

	@Override
	public IUIComponent getParentComp() {
		return parentComp;
	}

	@Override
	public void setParentComp(IUIComponent comp) {
		parentComp = comp;
	}



}